<template>
    <view>
        <!-- pages/user/address/index.wxml -->
        <!-- 头部 -->
        <navber id="head-navber" :navbarData="navbarData"></navber>

        <!-- 地址列表 -->
        <view class="address">
            <view class="address-item" v-for="(item, index) in addressData" :key="index">
                <view :class="'address-item-layer ' + (select == item.id ? 'select' : '')" @tap="onSelect($event, { id: item.id })" :data-id="item.id">
                    <view class="address-item-info" @tap="selectAddress($event, { index })" :data-index="index">
                        <view class="address-item-info-name">{{ item.take_name }}</view>
                        <view class="address-item-info-phone">{{ item.take_phone }}</view>
                        <view class="address-item-info-default" v-if="item.is_default == 1">默认</view>
                    </view>
                    <view class="address-item-detail" @tap="selectAddress($event, { index })" :data-index="index">
                        {{ item.province }} {{ item.city }} {{ item.district }} {{ item.detail_address }}
                    </view>
                    <view class="address-item-edit" @tap="edit_address($event, { id: item.id })" :data-id="item.id">
                        <image :src="imgUrl + '/imgs/user/address/edit.png'"></image>
                    </view>
                </view>

                <view class="address-item-delete" @tap="delAddress($event, { ids: item.id })" :data-ids="item.id">删除</view>
            </view>
        </view>

        <!-- 添加 -->
        <view class="add" @tap="add_address">
            <view class="add-img">
                <image :src="imgUrl + '/imgs/user/address/add.png'"></image>
            </view>
            <view class="add-text">添加地址</view>
        </view>
    </view>
</template>

<script>
import navber from '@/components/navber/index';
// pages/user/address/index.js
const Config = require('../../../config/index.js');

const Api = require('../../../api/index.js');

export default {
    components: {
        navber
    },
    data() {
        return {
            navbarData: {
                title: '我的地址' //顶部标题
            },
            //导航栏
            imgUrl: Config.imgUrl,
            //图片url
            addressData: [],
            is_order_select: false,
            //是否订单选择地址
            select: 0
        };
    },
    onLoad(e) {
        // 监听acceptDataFromOpenerPage事件，获取上一页面通过eventChannel传送到当前页面的数据
        const eventChannel = this.getOpenerEventChannel();

        if (eventChannel.on) {
            var _this = this;

            eventChannel.on('set_order_select', function (data) {
                _this.setData({
                    is_order_select: true
                });
            });
        }
    },
    onShow(e) {
        this.getData();
    },
    onPullDownRefresh(e) {
        this.getData();
        uni.stopPullDownRefresh();
    },
    methods: {
        onSelect(e, _dataset) {
            
            if (!e.currentTarget) {
                if (_dataSet.tagId) {
                    e.currentTarget = {
                        id: _dataSet.tagId
                    };
                } else {
                    e.currentTarget = {
                        dataset: _dataSet
                    };
                }
            }
            
            this.setData({
                select: e.currentTarget.dataset.id
            });
        },

        edit_address(e, _dataset) {
            
            if (!e.currentTarget) {
                if (_dataSet.tagId) {
                    e.currentTarget = {
                        id: _dataSet.tagId
                    };
                } else {
                    e.currentTarget = {
                        dataset: _dataSet
                    };
                }
            }
            
            //修改地址
            uni.navigateTo({
                url: 'edit/index?type=edit&id=' + e.currentTarget.dataset.id
            });
        },

        add_address(e, _dataset) {
            
            if (!e.currentTarget) {
                if (_dataSet.tagId) {
                    e.currentTarget = {
                        id: _dataSet.tagId
                    };
                } else {
                    e.currentTarget = {
                        dataset: _dataSet
                    };
                }
            }
            
            //添加地址
            uni.navigateTo({
                url: 'edit/index?type=add'
            });
        },

        getData(e) {
            //获取数据
            Api.userAddressList().then((res) => {
                this.setData({
                    addressData: res.data
                });
            }); // wx.stopPullDownRefresh();
        },

        delAddress(e, _dataset) {
            
            if (!e.currentTarget) {
                if (_dataSet.tagId) {
                    e.currentTarget = {
                        id: _dataSet.tagId
                    };
                } else {
                    e.currentTarget = {
                        dataset: _dataSet
                    };
                }
            }
            
            //删除地址
            var json = {
                id: e.currentTarget.dataset.ids
            };
            Api.userAddressDelete(json).then((res) => {
                if (res.code == 1) {
                    this.getData();
                }
            });
        },

        selectAddress(e, _dataset) {
            
            if (!e.currentTarget) {
                if (_dataSet.tagId) {
                    e.currentTarget = {
                        id: _dataSet.tagId
                    };
                } else {
                    e.currentTarget = {
                        dataset: _dataSet
                    };
                }
            }
            
            //订单选择地址
            if (this.is_order_select) {
                var data = this.addressData;
                var index = e.currentTarget.dataset.index;
                const eventChannel = this.getOpenerEventChannel();
                eventChannel.emit('addressInfo', {
                    data: data[index]
                });
                uni.navigateBack({
                    delta: 1
                });
            }
        }
    }
};
</script>
<style>
@import './index.css';
</style>
